/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/add-two-numbers
   @Language: C++
   @Datetime: 19-05-16 10:21
   */

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Solution {
public:
	ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
		if(l1==NULL || l2==NULL) return l1?l1:l2;
		ListNode H(0), *p=&H;
		for(int carry=0; l1 || l2 || carry; carry/=10){
			if(l1) carry+=l1->val, l1=l1->next;
			if(l2) carry+=l2->val, l2=l2->next;
			p=p->next=new ListNode(carry%10);
		}
		return H.next;
	}
};
